package cn.gz.ibix.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import cn.gz.ibix.R;
import cn.gz.ibix.adapter.FoodAdapter;
import cn.gz.ibix.bean.Food;
import cn.gz.ibix.load.FoodLoader;
import cn.gz.ibix.load.FoodLoaderManager;

/**
 * 项目名称：QiYiChuAPP
 * <br>类描述：
 * <br>创建人：htliu
 * <br>创建时间：2018/3/24 20:16
 * <br>修改人:
 * <br>修改时间：2018/3/24 20:16
 * <br>修改备注：
 * <br>@version
 */

public class FoodFragment extends Fragment {
    private static final String TAG = "FoodFragment";
    private FoodLoader foodLoader;
    private RecyclerView foodListView;

    public static FoodFragment newInstance(long menuID) {
        Bundle args = new Bundle();
        args.putLong("menuID", menuID);
        FoodFragment fragment = new FoodFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        foodLoader = FoodLoaderManager.getInstance().get(getArguments().getLong("menuID"));
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_food, container, false);
        foodListView = view.findViewById(R.id.list);
        initFoodList();
        return view;
    }

    private void initFoodList() {
        final FoodAdapter adapter = new FoodAdapter();
        foodLoader.setListener(new FoodLoader.Listener<Food>() {
            @Override
            public void success(List<Food> list) {
                Log.d(TAG, "success() called with: list = [" + list + "]");
                adapter.setNewData(list);
                if (foodLoader.isEnd()) {
                    adapter.loadMoreEnd();
                } else {
                    adapter.loadMoreComplete();
                }
            }

            @Override
            public void fail(Exception e) {
                Log.d(TAG, "fail() called with: e = [" + e + "]");
            }
        });
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                foodLoader.load();
                Log.d(TAG, "onLoadMoreRequested() called");
            }
        }, foodListView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        foodListView.setLayoutManager(layoutManager);
        foodListView.setAdapter(adapter);
        foodLoader.load();
    }

}
